package com.key.win.business.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.page.MybatisPageServiceTemplate;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.business.chat.dao.ImMessageDao;
import com.key.win.business.chat.eums.ImMessageTargetEnum;
import com.key.win.business.chat.model.ImMessage;
import com.key.win.business.chat.model.ImMessageDetail;
import com.key.win.business.chat.service.ImMessageDetailService;
import com.key.win.business.chat.service.ImMessageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImMessageServiceImpl extends ServiceImpl<ImMessageDao, ImMessage> implements ImMessageService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImMessageDetailService imMessageDetailService;

    public PageResult<ImMessage> findMessageByPaged(PageRequest<ImMessage> t) {
        MybatisPageServiceTemplate<ImMessage, ImMessage> query = new MybatisPageServiceTemplate<ImMessage, ImMessage>(this.baseMapper) {
            @Override
            protected AbstractWrapper constructWrapper(ImMessage message) {
                LambdaQueryWrapper<ImMessage> lambdaQueryWrapper = buildWrapper(message);
                return lambdaQueryWrapper;
            }
        };
        return query.doPagingQuery(t);
    }

    private LambdaQueryWrapper<ImMessage> buildWrapper(ImMessage imMessage) {
        if (imMessage == null) {
            logger.error("Message is null!!");
            throw new IllegalArgumentException("查询条件为空！");
        }
        if (StringUtils.isBlank(imMessage.getFromUserId())) {
            logger.error("Message fromUserId is null!!");
            throw new IllegalArgumentException("发送人为空！");
        }
        if (imMessage.getTarget() == null) {
            logger.error("Message target is null!!");
            throw new IllegalArgumentException("目标条件为空！");
        }
        LambdaQueryWrapper<ImMessage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ImMessage::getFromUserId, imMessage.getFromUserId());
        if (StringUtils.isNotBlank(imMessage.getContent())) {
            lambdaQueryWrapper.like(ImMessage::getContent, imMessage.getContent());
        }
        return lambdaQueryWrapper;
    }

    public List<ImMessage> findMessage(ImMessage imMessage) {
        return this.list(this.buildWrapper(imMessage));
    }


    public ImMessage addMessage(ImMessage imMessage) {
        if (imMessage == null) {
            logger.error("Message is null!!");
            throw new IllegalArgumentException("消息为空！");
        }
        if (StringUtils.isBlank(imMessage.getFromUserId())) {
            logger.error("Message fromUserId is null!!");
            throw new IllegalArgumentException("发送人为空！");
        }
        if (imMessage.getTarget() == null) {
            logger.error("Message target is null!!");
            throw new IllegalArgumentException("消息目标为空！");
        }
        if (imMessage.getType() == null) {
            logger.error("Message type is null!!");
            throw new IllegalArgumentException("消息类型为空！");
        }
        if (this.save(imMessage)) {
            return imMessage;
        }
        return null;
    }

    @Async
    public Boolean addFriendApply(ImMessage imMessage, boolean status, String toUserId) {
        return this.addMessage(imMessage, status, toUserId);
    }

    @Async
    public Boolean addGroupApply(ImMessage imMessage, boolean status, String toUserId) {
        return this.addMessage(imMessage, status, toUserId);
    }

    public Boolean addMessage(ImMessage imMessage, boolean status, String toUserId) {
        if (StringUtils.isBlank(imMessage.getId())) {
            if (this.addMessage(imMessage) == null) {
                return Boolean.FALSE;
            }
        }
        ImMessageDetail imMessageDetail = new ImMessageDetail();
        imMessageDetail.setStatus(status);
        imMessageDetail.setFromUserId(imMessage.getFromUserId());
        imMessageDetail.setType(imMessage.getType());
        imMessageDetail.setTarget(imMessage.getTarget());
        imMessageDetail.setTargetId(imMessage.getTargetId());
        imMessageDetail.setToUserId(toUserId);
        imMessageDetail.setMessageId(imMessage.getId());
        return imMessageDetailService.addMessageDetail(imMessageDetail);
    }

    public Boolean deleteMessage(String messageDetailId) {
        ImMessageDetail byId = imMessageDetailService.getById(messageDetailId);
        if (imMessageDetailService.removeById(messageDetailId)) {
            if (byId != null && (ImMessageTargetEnum.FRIEND == byId.getTarget() || ImMessageTargetEnum.APPLY == byId.getTarget() || ImMessageTargetEnum.SYSTEM == byId.getTarget())) {
                return this.removeById(byId.getMessageId());
            }
        }
        return Boolean.FALSE;
    }
}
